package com.chaodev.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.chaodev.base.dao.AdviceDao;
import com.chaodev.base.dto.AdviceDto;
import com.chaodev.base.dto.AdviceReplyDto;
import com.chaodev.base.model.Advice;
import com.chaodev.base.model.AdviceReply;
import com.chaodev.base.service.AdviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * vcbase
 *
 * @author 95634
 * @描述
 * @date 2023/5/18 9:04
 **/
@Service
public class AdviceServiceImpl implements AdviceService {

    @Autowired
    AdviceDao adviceDao;

    @Override
    public Advice findDataById(String id) {
        return adviceDao.findDataById(id);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void saveData(Advice tbAdvice) throws DataAccessException {
        if(ObjectUtil.isNotNull(tbAdvice.getId())&&tbAdvice.getId()>0){
            adviceDao.updateDataById(tbAdvice);
        }else{
            adviceDao.insertData(tbAdvice);
        }

    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void insertData(Advice tbAdvice) throws DataAccessException {
        adviceDao.insertData(tbAdvice);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void updateDataById(Advice tbAdvice) throws DataAccessException {
        adviceDao.updateDataById(tbAdvice);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deleteDataById(String id) throws DataAccessException {
        adviceDao.deleteDataById(id);
    }

    @Override
    public List<Advice> queryDataList(Advice tbAdvice) {
        return adviceDao.queryDataList(tbAdvice);
    }

    @Override
    public PageInfo<Advice> queryPageList(AdviceDto dto) {
        PageHelper.startPage(dto.getPage(), dto.getRows());
        Advice advice=new Advice();
        BeanUtil.copyProperties(dto,advice);
        List<Advice> tbAdviceList = adviceDao.queryDataList(advice);
        PageInfo<Advice> pageInfo = new PageInfo<Advice>(tbAdviceList, dto.getRows());
        return pageInfo;
    }

    @Override
    public AdviceReply findReplyDataById(String id) {
        return adviceDao.findReplyDataById(id);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void saveReplyData(AdviceReply tbAdviceReply) throws DataAccessException{
        if(ObjectUtil.isNotNull(tbAdviceReply.getId()) &&tbAdviceReply.getId()>0){
            adviceDao.updateReplyDataById(tbAdviceReply);
        }else{
            adviceDao.insertReplyData(tbAdviceReply);
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void insertReplyData(AdviceReply tbAdviceReply) throws DataAccessException{
        adviceDao.insertReplyData(tbAdviceReply);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void updateReplyDataById(AdviceReply tbAdviceReply) throws DataAccessException{
        adviceDao.updateReplyDataById(tbAdviceReply);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deleteReplyDataById(String id) throws DataAccessException{
        adviceDao.deleteReplyDataById(id);
    }

    @Override
    public List<AdviceReply> queryReplyDataList(AdviceReply tbAdviceReply) {
        return adviceDao.queryReplyDataList(tbAdviceReply);
    }

    @Override
    public PageInfo<AdviceReply> queryReplyPageList(AdviceReplyDto dto) {
        PageHelper.startPage(dto.getPage(), dto.getRows());
        AdviceReply adviceReply = new AdviceReply();
        BeanUtil.copyProperties(dto,adviceReply);
        List<AdviceReply> tbAdviceReplyList = adviceDao.queryReplyDataList(adviceReply);
        PageInfo<AdviceReply> pageInfo = new PageInfo<>(tbAdviceReplyList, dto.getRows());
        return pageInfo;
    }
}
